package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Topic;
import com.fz.entity.TopicComment;
import com.fz.mapper.TopicCommentMapper;
import com.fz.service.TopicCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 存储话题评论信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class TopicCommentServiceImpl extends ServiceImpl<TopicCommentMapper, TopicComment> implements TopicCommentService {
    // 获取话题下的评论（按时间倒序）
    public List<TopicComment> getCommentsByTopicId(Long topicId) {
        QueryWrapper<TopicComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topic_id", topicId)
                .eq("is_deleted", 0)
                .orderByDesc("create_time");
        List<TopicComment> list = super.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean likeComment(Long commentId) {
        UpdateWrapper<TopicComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("likecount = likecount + 1")
                .eq("comment_id", commentId);
        if(this.update(updateWrapper)){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public TopicComment createComment(Long topicId,Long userId,String content){
        TopicComment topicComment=new TopicComment(topicId,userId,content);
        this.save(topicComment);
        return topicComment;
    }
}
